package com.growthserver.util;

import com.growthserver.excelFileEntity.IntegralBudgetExclEntity;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExclUtils {

    /**
     * 解析excl
     *
     * @param file
     * @return
     * @throws IOException
     */
    public static List<String> getExclData(MultipartFile file) throws IOException {
        ArrayList<String> arrayList = new ArrayList<>();
        // 使用XSSFWorkbook加载Excel文件
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        // 跳过第一行
        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            try {
                arrayList.add(row.getCell(0).getStringCellValue());
            } catch (Exception e) {
                String integralBudgetNum = String.valueOf(row.getCell(0).getNumericCellValue());
                arrayList.add(integralBudgetNum.split("\\.")[0]);
            }
        }
        workbook.close();
        return arrayList;
    }

    /**
     *  excel文件导入 对象接收
     * @param file
     * @return
     * @throws IOException
     */
    public static List<IntegralBudgetExclEntity> getIntegralBudgetExclData(MultipartFile file) throws IOException {
        List<IntegralBudgetExclEntity> dataList = new ArrayList<>();
        // 使用XSSFWorkbook加载Excel文件
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheet = workbook.getSheetAt(0);
        // 跳过第一行
        for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
            Row row = sheet.getRow(rowNum);
            IntegralBudgetExclEntity data = new IntegralBudgetExclEntity();
            try {
                data.setIntegralBudgetNum(row.getCell(0).getStringCellValue());
            } catch (Exception e) {
                String integralBudgetNum = String.valueOf(row.getCell(0).getNumericCellValue());
                data.setIntegralBudgetNum(integralBudgetNum.split("\\.")[0]);
            }
            try {
                data.setBudgetName(String.valueOf(row.getCell(1).getStringCellValue()));
            } catch (Exception e) {
                String budgetName = String.valueOf(row.getCell(1).getNumericCellValue());
                data.setBudgetName(budgetName.split("\\.")[0]);
            }
            try {
                data.setBudgetDistributeDepartment(row.getCell(2).getStringCellValue());
            } catch (Exception e) {
                String budgetDistributeDepartment = String.valueOf(row.getCell(2).getNumericCellValue());
                data.setBudgetDistributeDepartment(budgetDistributeDepartment.split("\\.")[0]);
            }
            try {
                data.setBudgetType(row.getCell(3).getStringCellValue());
            } catch (Exception e) {
                String budgetType = String.valueOf(row.getCell(3).getNumericCellValue());
                data.setBudgetType(budgetType.split("\\.")[0]);
            }
            try {
                data.setAuditStatus(row.getCell(4).getStringCellValue());
            } catch (Exception e) {
                String stringCellValue = String.valueOf(row.getCell(4).getNumericCellValue());
                data.setAuditStatus(stringCellValue.split("\\.")[0]);
            }
            try {
                data.setBudgetIntegralValue(row.getCell(5).getStringCellValue());
            } catch (Exception e) {
                String budgetIntegralValue = String.valueOf(row.getCell(5).getNumericCellValue());
                data.setBudgetIntegralValue(budgetIntegralValue.split("\\.")[0]);
            }
            dataList.add(data);
        }
        workbook.close();
        return dataList;
    }
}
